package com.example.demo.service.impl;

import com.example.demo.mapper.EmpMapper;
import com.example.demo.pojo.Emp;
import com.example.demo.pojo.PageBean;
import com.example.demo.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    EmpMapper empMapper;
    @Override
    public PageBean get(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {

        /*Long l1 = empMapper.count();
        Integer start = (page - 1) * pageSize;
        List<Emp> l2 = empMapper.page(start,pageSize);
        PageBean pageBean = new PageBean();
        pageBean.setRows(l2);
        pageBean.setTotal(l1);
        return pageBean;*/

        //PageHelper插件

        PageHelper.startPage(page,pageSize);
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) empList;
        PageBean pageBean= new PageBean(p.getTotal(),p.getResult());
        return pageBean;



    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);

    }

    @Override
    public void add(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        empMapper.add(emp);
    }

    @Override
    public Emp select(Integer id) {
        return empMapper.select(id);
    }

    @Override
    public void modify(Emp emp) {
        empMapper.modify(emp);
    }

    @Override
    public void deleteById(Integer id) {
        empMapper.deleteById(id);
    }


    @Override
    public Emp login(Emp emp) {
        Emp emp1 = empMapper.login(emp);
        return emp1;
    }




}
